package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

func getquery(c *gin.Context) {
	//获取query参数
	name := c.Query("name")
	age := c.Query("age")
	//返回json
	c.JSON(200, gin.H{
		"name": name,
		"age":  age,
	})
}

func getform(c *gin.Context) {
	c.HTML(http.StatusOK, "login.html", nil)
}

func showform(c *gin.Context) {
	//获取表单参数
	username := c.PostForm("username")
	password := c.PostForm("password")
	//返回json
	c.JSON(200, gin.H{
		"username": username,
		"password": password,
	})
}

func getpath(c *gin.Context) {
	//获取路径参数
	username := c.Param("username")
	//返回json
	c.JSON(200, gin.H{
		"username": username,
	})
}

func main() {
	r := gin.Default()
	r.LoadHTMLGlob("templates/*")
	r.GET("/", getquery)
	r.GET("/form", getform)
	r.POST("/form", showform)
	r.GET("/path/:username", getpath)
	r.Run(":80")
}
